package com.googlecode.gwtresume.client.render;



import java.util.Iterator;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.gwtresume.client.Renderer;
import com.googlecode.gwtresume.client.api.Header;
import com.googlecode.gwtresume.client.api.Position;
import com.googlecode.gwtresume.client.api.Project;
import com.googlecode.gwtresume.client.api.Skill;
import com.googlecode.gwtresume.client.api.SkillSet;
import com.googlecode.gwtresume.client.widgets.DefinitionList;
import com.googlecode.gwtresume.client.widgets.ListItem;
import com.googlecode.gwtresume.client.widgets.UnorderedList;

public class ClassicRenderer extends BaseRenderer implements Renderer {
	
	

	public ClassicRenderer() {
		super();
	}
	
	public void render(VerticalPanel content) {
		FlowPanel header = newPanel("header", content);
		FlowPanel contact_info = newPanel("contact_info", header);
		FlowPanel body = newPanel("body", content);
		FlowPanel skills = newPanel("skills", body);
		FlowPanel experience = newPanel("experience", body);
		FlowPanel experience_summary = newPanel("experience_summary",
				experience);
		FlowPanel experience_detail = newPanel("experience_detail", experience);
		FlowPanel footer = newPanel("footer", content);

		Header rheader = resume.getHeader();
		
		UnorderedList contact = new UnorderedList("contact_list");
		
		contact.add(new ListItem("name", getFullName()));
		contact.add(new ListItem("address", new InlineHTML(concat(constants
				.delimiterHtml(), rheader.getAddress()))));
		contact.add(new ListItem("phone", new InlineHTML(
				concat(constants.delimiterHtml(), formatPhone(resume.getHeader().getPhone()), getEmail().toString()))));

		contact.add(new Tag("li","resume_controls", renderControls()));


		contact_info.add(contact);

		
		
		experience_summary.add(new Tag("h1", constants.profile()));

		experience_summary.add(new Tag("p", resume.getSummary()));

		try {
			skills.add(new Tag("h1", constants.skills()));

			for (SkillSet skillSet : resume.getSkills()) {
				skills.add(new Tag("h3", skillSet.getTitle()));
				UnorderedList rootList = new UnorderedList("skill_list");		
				UnorderedList ul = new UnorderedList();
				rootList.add(ul);
				Iterator<Skill> it = getSortedSkills(skillSet.getSkills()).iterator();
				while (it.hasNext()) {
					Skill skill = it.next();
					ul.add(new ListItem(skill.getSkillLevel().toString(), skill.getName() + (it.hasNext() ? ", " : "")));
				}
				

				skills.add(rootList);

			}

			skills.add(new Tag("h1", constants.education()));

			for (String[] credit : resume.getEducation()) {
				UnorderedList edul = new UnorderedList("education_list");
				for (String line : credit) {
					edul.add(new ListItem(line));
				}
				skills.add(edul);
			}

			experience.add(new Tag("h1", constants.experience()));
			for (Position p : resume.getExperience()) {
				FlowPanel employmentInfo = new FlowPanel();
				employmentInfo.setStylePrimaryName("employment_info");

				UnorderedList pul = new UnorderedList("position_list");

				String url = p.getUrl();
				Widget title = null;
				if (url == null) {
					title = new InlineHTML(p.getEmployer()
							+ constants.delimiterHtml() + p.getTitle());
				} else {
					FlowPanel fp = new FlowPanel();
					fp.add(new Anchor(p.getEmployer(), false, p.getUrl()));
					fp.add(new InlineHTML(constants.delimiterHtml()
							+ p.getTitle()));
					// title = new InlineHTML(.getHTML() + );
					title = fp;
				}

				pul.add(new ListItem("title", title));
				pul.add(new ListItem("employment_period", getPeriod(p)));

				employmentInfo.add(pul);

				experience.add(employmentInfo);

				experience.add(new Tag("p", "experience_description", p
						.getDescription()));

				FlowPanel panel = new FlowPanel();
				panel.setStylePrimaryName("experience_info");

				DefinitionList dl = new DefinitionList();
				dl.setStylePrimaryName("projects");
				for (Project project : p.getProjects()) {
					dl.add(project.getName(), project
							.getDescription(moreDetail));

				}
				panel.add(dl);
				experience.add(panel);

			}
			
			footer.add(renderFooter());
			
		} catch (Throwable x) {
			x.printStackTrace();
		}

	}

}
